package aes.gui;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;

import aes.general.Exam;
import aes.general.GuiControlBeta;
import aes.general.StudentControl;


/**
 * The Class StudentMakeExamAutomated.
 * This panel is intended for the students who are making the 
 * Automatic exam. The Panel displays questions for student and student selects 
 * the possible answer.   
 *  
 */
public class StudentMakeExamAutomated extends JPanel implements ActionListener {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static JPanel cp = null;
	private JPanel topPane;
	private JPanel questionPane;
	private JPanel navigationPane;
	private JPanel answerPane;
	private JPanel CommentsPane;
	private JLabel timeLeft;
	private JLabel label;
	private JLabel label_1;
	private JLabel lblQuestion;
	private JLabel lblDescription;
	private JLabel lblAnswer;
	private JLabel lblAnswer_1;
	private JLabel lblAnswer_2;
	private JLabel lblAnswer_3;
	private JLabel lblSelectCorrectAnswer;
	private JLabel lblTeachersComments;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;
	private JButton btnNewButton;
	private JRadioButton radioButtonAnswer1;
	private JRadioButton radioButtonAnswer2;
	private JRadioButton radioButtonAnswer3;
	private JRadioButton radioButtonAnswer4;
	private ButtonGroup group;
	private JTextArea qstTextArea ;
	private JTextArea textAreaAnswer1 ;
	private JTextArea textAreaAnswer2 ;
	private JTextArea textAreaAnswer3 ;
	private JTextArea textAreaAnswer4 ;
	private JTextArea textAreaDescription;
	private JTextArea textAreaComments;
	private int currentQstNumInArr=0;
	private int selectedAns;
	private static Timer timer;
	private static Timer timer2;
	private static Exam aExam=null;
	private static long examduration;
	private static long actualStudentduration;
	private static boolean timeAddedFlag=false;
	private static boolean examlocked=false;
	private static long timeToAdd;
	
	
	/**
	 * Create the panel.
	 *
	 * @param contentPane the content pane
	 * @param autoExam the automatic Exam  
	 */
	public StudentMakeExamAutomated(JPanel contentPane, Exam autoExam) {
		this.cp = contentPane;
		this.aExam = autoExam;
		this.examlocked = false;
		this.examduration = autoExam.getDuration() ;
		this.actualStudentduration = 0;
		timeAddedFlag = false;
		aExam.setStudentId(GuiControlBeta.userid);
		
		setLayout(null);
		
		topPane = new JPanel();
		topPane.setBounds(20, 11, 624, 44);
		add(topPane);
		topPane.setLayout(null);
		
		label = new JLabel("Minutes");
		label.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		label.setBackground(Color.BLACK);
		label.setBounds(207, 11, 169, 29);
		topPane.add(label);
		
		label_1 = new JLabel("Time Left : ");
		label_1.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		label_1.setBackground(Color.BLACK);
		label_1.setBounds(10, 11, 169, 29);
		topPane.add(label_1);
		
		timeLeft = new JLabel(aExam.getDuration().toString());
		timeLeft.setForeground(Color.RED);
		timeLeft.setFont(new Font("Arial", Font.BOLD, 25));
		timeLeft.setBackground(Color.RED);
		timeLeft.setBounds(130, 0, 65, 45);
		topPane.add(timeLeft);
		
		questionPane = new JPanel();
		questionPane.setBounds(20, 55, 624, 383);
		add(questionPane);
		questionPane.setLayout(null);
		
		qstTextArea = new JTextArea();
		qstTextArea.setEditable(false);
		qstTextArea.setBounds(0, 24, 624, 148);
		questionPane.add(qstTextArea);
		
		textAreaDescription = new JTextArea();
		textAreaDescription.setEditable(false);
		textAreaDescription.setBounds(0, 199, 624, 44);
		questionPane.add(textAreaDescription);
		
		lblQuestion = new JLabel("Question:");
		lblQuestion.setBounds(0, 11, 83, 14);
		questionPane.add(lblQuestion);
		
		lblDescription = new JLabel("Description :");
		lblDescription.setBounds(0, 183, 134, 14);
		questionPane.add(lblDescription);
		
		lblAnswer = new JLabel("Answer 1");
		lblAnswer.setBounds(0, 254, 67, 14);
		questionPane.add(lblAnswer);
		
		lblAnswer_1 = new JLabel("Answer 4");
		lblAnswer_1.setBounds(0, 358, 67, 14);
		questionPane.add(lblAnswer_1);
		
		lblAnswer_2 = new JLabel("Answer 2");
		lblAnswer_2.setBounds(0, 291, 83, 14);
		questionPane.add(lblAnswer_2);
		
		lblAnswer_3 = new JLabel("Answer 3");
		lblAnswer_3.setBounds(0, 327, 67, 14);
		questionPane.add(lblAnswer_3);
		
		textAreaAnswer1 = new JTextArea();
		textAreaAnswer1.setEditable(false);
		textAreaAnswer1.setBounds(90, 254, 534, 24);
		questionPane.add(textAreaAnswer1);
		
		textAreaAnswer2 = new JTextArea();
		textAreaAnswer2.setEditable(false);
		textAreaAnswer2.setBounds(90, 291, 534, 24);
		questionPane.add(textAreaAnswer2);
		
		textAreaAnswer3 = new JTextArea();
		textAreaAnswer3.setEditable(false);
		textAreaAnswer3.setBounds(90, 327, 534, 24);
		questionPane.add(textAreaAnswer3);
		
		textAreaAnswer4 = new JTextArea();
		textAreaAnswer4.setEditable(false);
		textAreaAnswer4.setBounds(90, 358, 534, 24);
		questionPane.add(textAreaAnswer4);
		
		navigationPane = new JPanel();
		navigationPane.setBounds(20, 577, 624, 57);
		add(navigationPane);
		
		btnNewButton = new JButton("Previous Question");
		btnNewButton.setBounds(84, 5, 142, 23);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				selectPreviousQuestion();
			}
		});
		navigationPane.setLayout(null);
		navigationPane.add(btnNewButton);
		
		btnNewButton_1 = new JButton("Submit");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				submitExam();
			}
		});
		btnNewButton_1.setBounds(262, 24, 89, 23);
		navigationPane.add(btnNewButton_1);
		
		btnNewButton_2 = new JButton("Next Question");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				selectNextQuestion();
				
			}
		});
		btnNewButton_2.setBounds(394, 5, 142, 23);
		navigationPane.add(btnNewButton_2);
		
		answerPane = new JPanel();
		answerPane.setBounds(20, 465, 624, 34);
		add(answerPane);
		answerPane.setLayout(null);
		
		radioButtonAnswer1 = new JRadioButton("Answer 1");
		radioButtonAnswer1.setBounds(24, 7, 109, 23);
		answerPane.add(radioButtonAnswer1);
		
		radioButtonAnswer2 = new JRadioButton("Answer 2");
		radioButtonAnswer2.setBounds(153, 7, 109, 23);
		answerPane.add(radioButtonAnswer2);
		
		radioButtonAnswer3 = new JRadioButton("Answer 3");
		radioButtonAnswer3.setBounds(316, 7, 109, 23);
		answerPane.add(radioButtonAnswer3);
		
		radioButtonAnswer4 = new JRadioButton("Answer 4");
		radioButtonAnswer4.setBounds(458, 7, 109, 23);
		answerPane.add(radioButtonAnswer4);
			
		group = new ButtonGroup();
		group.add(radioButtonAnswer1);
		group.add(radioButtonAnswer2);
		group.add(radioButtonAnswer3);
		group.add(radioButtonAnswer4);
		
		radioButtonAnswer1.addActionListener(this);
		radioButtonAnswer2.addActionListener(this);
		radioButtonAnswer3.addActionListener(this);
		radioButtonAnswer4.addActionListener(this);
		
		lblSelectCorrectAnswer = new JLabel("Select Correct Answer : ");
		lblSelectCorrectAnswer.setBounds(20, 451, 209, 14);
		add(lblSelectCorrectAnswer);
		
		CommentsPane = new JPanel();
		CommentsPane.setBounds(20, 524, 624, 42);
		add(CommentsPane);
		CommentsPane.setLayout(null);
		
		textAreaComments = new JTextArea(aExam.getComments());
		textAreaComments.setEditable(false);
		textAreaComments.setBounds(0, 0, 624, 42);
		CommentsPane.add(textAreaComments);
		
		lblTeachersComments = new JLabel("Teacher's Comments :");
		lblTeachersComments.setBounds(20, 510, 140, 14);
		add(lblTeachersComments);
		
		
		for (int i=0;i<aExam.getQstManualExam().size();i++)
		{
			 aExam.getQstManualExam().get(i).setSelectedAnsExAuto(0);	
		}
		initfirstqst();
		
		timerfunction();
	}
	
	private void initfirstqst() {
		qstTextArea.setText(	aExam.getQstManualExam().get(currentQstNumInArr).getQuestion() );
		textAreaDescription.setText(aExam.getQstManualExam().get(currentQstNumInArr).getDescription());
		textAreaAnswer1.setText(aExam.getQstManualExam().get(currentQstNumInArr).getAnswer().get(0));
		textAreaAnswer2.setText(aExam.getQstManualExam().get(currentQstNumInArr).getAnswer().get(1));
		textAreaAnswer3.setText(aExam.getQstManualExam().get(currentQstNumInArr).getAnswer().get(2));
		textAreaAnswer4.setText(aExam.getQstManualExam().get(currentQstNumInArr).getAnswer().get(3));
	}

	/**
	 * Select next question.Displays Next Question in Exam
	 */
	protected void selectNextQuestion() {
		if (group.isSelected(null)) {
			JOptionPane.showMessageDialog(null, "Select any possible Answer", "Error",
					JOptionPane.ERROR_MESSAGE);
					return;
					}
		
		if (currentQstNumInArr<aExam.getQstManualExam().size()-1) 
		{
			currentQstNumInArr++;
			
			group.clearSelection();
			Integer i=aExam.getQstManualExam().get(currentQstNumInArr).getSelectedAnsExAuto();
			if (i!=null) {
				if (i==1) radioButtonAnswer1.setSelected(true);
				if (i==2) radioButtonAnswer2.setSelected(true);
				if (i==3) radioButtonAnswer3.setSelected(true);
				if (i==4) radioButtonAnswer4.setSelected(true);
						}
			qstTextArea.setText(aExam.getQstManualExam().get(currentQstNumInArr).getQuestion() );
			textAreaDescription.setText(aExam.getQstManualExam().get(currentQstNumInArr).getDescription());
			textAreaAnswer1.setText(aExam.getQstManualExam().get(currentQstNumInArr).getAnswer().get(0));
			textAreaAnswer2.setText(aExam.getQstManualExam().get(currentQstNumInArr).getAnswer().get(1));
			textAreaAnswer3.setText(aExam.getQstManualExam().get(currentQstNumInArr).getAnswer().get(2));
			textAreaAnswer4.setText(aExam.getQstManualExam().get(currentQstNumInArr).getAnswer().get(3));
		}
		else  {
				JOptionPane.showMessageDialog(null, "No more Question", "Error",
					JOptionPane.ERROR_MESSAGE);
					return;
				}
	}

	/**
	 * Submit exam method. 
	 */
	protected void submitExam() {
		aExam.setActualStudentExamDuration(String.valueOf(actualStudentduration) ); 
		aExam.setFinishedintime("y");		
		if (group.isSelected(null)) {
			JOptionPane.showMessageDialog(null, "Select any possible Answer", "Error",
					JOptionPane.ERROR_MESSAGE);
					return;
					}
		JOptionPane.showMessageDialog(null, "Exam submitted,Good Luck", "INFORMATION_MESSAGE",
				JOptionPane.INFORMATION_MESSAGE);
		
			timer.cancel();  
			StudentControl.DeleteActivationCode(aExam.getExamId()); //delete activation code		  			
			StudentControl.SendAutomaticExam(aExam);
			CardLayout cl = (CardLayout) (cp.getLayout());
			cl.show(cp, "MainPanel");
	}

	/**
	 * Select previous question method.Displays previous Question in Exam
	 */
	protected void selectPreviousQuestion() {
		if (group.isSelected(null)) {
			JOptionPane.showMessageDialog(null, "Select any possible Answer", "Error",
					JOptionPane.ERROR_MESSAGE);
						return;
						}
		if (currentQstNumInArr>0) 
		{
			currentQstNumInArr--;
			
			group.clearSelection();
			int i=aExam.getQstManualExam().get(currentQstNumInArr).getSelectedAnsExAuto();
			if (i==1) radioButtonAnswer1.setSelected(true);
			if (i==2) radioButtonAnswer2.setSelected(true);
			if (i==3) radioButtonAnswer3.setSelected(true);
			if (i==4) radioButtonAnswer4.setSelected(true);
			
			qstTextArea.setText(	aExam.getQstManualExam().get(currentQstNumInArr).getQuestion() );
			textAreaDescription.setText(aExam.getQstManualExam().get(currentQstNumInArr).getDescription());
			textAreaAnswer1.setText(aExam.getQstManualExam().get(currentQstNumInArr).getAnswer().get(0));
			textAreaAnswer2.setText(aExam.getQstManualExam().get(currentQstNumInArr).getAnswer().get(1));
			textAreaAnswer3.setText(aExam.getQstManualExam().get(currentQstNumInArr).getAnswer().get(2));
			textAreaAnswer4.setText(aExam.getQstManualExam().get(currentQstNumInArr).getAnswer().get(3));
		}
		else  {
				JOptionPane.showMessageDialog(null, "This is a first Question", "Error",
					JOptionPane.ERROR_MESSAGE);
					return;
				}
	}

	/**
	 * Add time function.
	 *
	 * @param time the time in minutes to add to exam time
	 */
	public static void addtimefunc(String time)
	{
		timeAddedFlag=true;
		timeToAdd=Integer.valueOf(time);
		examduration+=timeToAdd;

		timer2 = new Timer();
		
		timer2.schedule(  new TimerTask() {
			  @Override
			  public void run() {	
							aExam.setActualStudentExamDuration(String.valueOf(actualStudentduration) ); 
							aExam.setFinishedintime("n");	
				  			JOptionPane.showMessageDialog(null, "Sorry.Time ended", "",
							JOptionPane.INFORMATION_MESSAGE);			  			
				  			StudentControl.DeleteActivationCode(aExam.getExamId()); //delete activation code		  			
				  			StudentControl.SendAutomaticExam(aExam);
				  			CardLayout cl = (CardLayout) (cp.getLayout());
				  			cl.show(cp, "MainPanel");
				  			cancel();
			  }
			}
		, TimeUnit.MINUTES.toMillis(examduration));
	}
	
	private void timerfunction() {
		
		timer = new Timer();
		
		timer.schedule(  new TimerTask() {
			  @Override
			  public void run() {
				  			
				  			if (timeAddedFlag==true) {cancel();return;}
							aExam.setActualStudentExamDuration(String.valueOf(actualStudentduration) ); 
							aExam.setFinishedintime("n");	
				  			JOptionPane.showMessageDialog(null, "Sorry.Time ended", "",
							JOptionPane.INFORMATION_MESSAGE);
				  			timer.cancel();  
				  			StudentControl.DeleteActivationCode(aExam.getExamId()); //delete activation code		  			
				  			StudentControl.SendAutomaticExam(aExam);
				  			CardLayout cl = (CardLayout) (cp.getLayout());
				  			cl.show(cp, "MainPanel");
			  }
			}
		, TimeUnit.MINUTES.toMillis(examduration));

		timer.scheduleAtFixedRate(  new TimerTask() {
			  @Override
			  public void run() {
				  		
				  examduration--;
				  actualStudentduration++;
				  timeLeft.setText(String.valueOf(examduration));
				  
			  }
			}
		,TimeUnit.MINUTES.toMillis(1),TimeUnit.MINUTES.toMillis(1));
		
		timer.scheduleAtFixedRate(  new TimerTask() {
			  @Override
			  public void run() {
				  		
				  	if (examlocked==true) 
				  	{
				  		
				  		StudentControl.DeleteActivationCode(aExam.getExamId()); //delete activation code		  			
			  			CardLayout cl = (CardLayout) (cp.getLayout());
			  			cl.show(cp, "MainPanel");
				  		timer.cancel();
				  		if(timer2!=null )timer2.cancel();
				  		
				  	}  
			  }
			}
		,1000,1000);	
		}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == radioButtonAnswer1)
		{
			 selectedAns = 1;
			 aExam.getQstManualExam().get(currentQstNumInArr).setSelectedAnsExAuto(selectedAns);
		}
			 
		if(e.getSource() == radioButtonAnswer2)
		{
			selectedAns = 2;
			aExam.getQstManualExam().get(currentQstNumInArr).setSelectedAnsExAuto(selectedAns);
		}	
		if(e.getSource() == radioButtonAnswer3)
		{
			selectedAns = 3;
			aExam.getQstManualExam().get(currentQstNumInArr).setSelectedAnsExAuto(selectedAns);
		}
		if(e.getSource() == radioButtonAnswer4)
		{
			selectedAns = 4;
			aExam.getQstManualExam().get(currentQstNumInArr).setSelectedAnsExAuto(selectedAns);
		}
	}

	/**
	 * Lock exam function. Changes the exam execution flag , listened by specific timer
	 * 
	 * @param string  
	 *       
	 */
	public static void lockexamfunc(String string) {
		examlocked=true;
	}
}
